package com.crois.barrier.web.plugin.before;

import com.crois.barrier.common.dto.ApiRouteInfo;
import com.crois.barrier.common.enums.PluginEnum;
import com.crois.barrier.common.enums.PluginTypeEnum;
import com.crois.barrier.plugin.BarrierPlugin;
import com.crois.barrier.plugin.BarrierPluginChain;
import com.crois.barrier.web.service.ParamMappingService;
import com.crois.barrier.web.support.ServerWebExchangeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author Hou Ze Yu
 * @description 参数映射插件
 * @date 2020/10/13
 */
public class ParamMappingPlugin implements BarrierPlugin {

    private static final Logger LOGGER = LoggerFactory.getLogger(ParamMappingPlugin.class);

    private ParamMappingService paramMappingService;

    public ParamMappingPlugin(ParamMappingService paramMappingService) {
        this.paramMappingService = paramMappingService;
    }

    @Override
    public Mono<Void> execute(ServerWebExchange exchange, BarrierPluginChain chain) {
        LOGGER.info("[开始执行：ParamMappingPlugin]");

        ApiRouteInfo apiRouteInfo = exchange.getAttribute(ServerWebExchangeUtils.BARRIER_ROUTE_ATTR);
        return paramMappingService.doParamMapping(apiRouteInfo,exchange,chain);
    }

    @Override
    public PluginTypeEnum pluginType() {
        return PluginTypeEnum.BEFORE;
    }

    @Override
    public Boolean skip(ServerWebExchange exchange) {
        return false;
    }

    @Override
    public String name() {
        return PluginEnum.PARAM_MAPPING.getName();
    }

    @Override
    public int order() {
        return PluginEnum.PARAM_MAPPING.getCode();
    }
}
